#ifndef STRING_H
#define STRING_H
#include <iostream>
#include <cstring>

using namespace std;

class String{
    private:
    char *str;
    size_t length;

    public:
    String()
    {
    length = 0;
    str = new char[1];
    str[0] = '\0';
    };//初始化空字符串
    String(const char *s)
    {
    length = strlen(s);
    str = new char[length+1];
    strcpy(str,s);
    };//初始化传进来的副本
    ~String()
    {
        delete [] str;
    };
    size_t getLength() const;//获取字符串长度  size_t长度类型 const不会改变这个类里的成员变量
    char operator [] (size_t index) const;//[]是一个运算符取下标，eg: s[2]
    String &operator = (const String& s);
    bool operator==(const String& s) const;
    bool operator!=(const String& s) const;
    String copy() const;//字符串拷贝
    String operator+(const String& s) const;//拼接字符串
    friend ostream& operator<<(ostream &out, const String& s);
};


size_t String::getLength() const{
    return length;
}

char String::operator[](size_t index) const{
    return str[index];
}

String& String::operator=(const String& s)
{
    if (this != &s){
        delete [] str;
        length = s.length;
        str = new char[length+1];
        strcpy(str,s.str);
    }
    return *this;
}

bool String::operator==(const String& s) const{
    return strcmp(str,s.str)==0;
}

bool String::operator!=(const String& s) const{
    return strcmp(str,s.str)!=0;
}

String String::copy() const{
    String s = *this;
    return s;
}

String String::operator+(const String& s) const{
    String result;
    result.length = length+s.length;
    result.str = new char[result.length+1];
    strcpy(result.str,str);
    strcat(result.str,s.str);
    return result;
}

ostream& operator<<(ostream& out, const String& s){
    out<<s.str;
    return out;
}

#endif